define(['app/mdSheQuHospitalBase/stock/stock.model', 'app/mdSheQuHospitalBase/stock/stock.service', 'hr.common'
, 'app/hrManagerRbac/dictionary/dictionary.service'
, 'app/mdSheQuHospitalBase/medicine/medicine.service'
], function (Model, service, common
, dictionaryService
, medicineService
) {
    function initialize(){
        var model = new Model();
        var initParam = model.tableModel.param();

        dictionaryService.findAll().success(function(data){
            model.dialogModel.dictionaryArray(data.data || []);
        });

        medicineService.findAll().success(function(data){
            model.dialogModel.medicineArray(data.data || []);
        });

        /**
         * 查询
         */
        model.searchData = function(data, event){
            var param = '';
            param += 'search_EQ_bid=' + ko.unwrap(data.searchModel.bid()) + '&';
            param += 'search_EQ_retailPrice=' + ko.unwrap(data.searchModel.retailPrice()) + '&';
            param += 'search_EQ_vipPrice=' + ko.unwrap(data.searchModel.vipPrice()) + '&';
            param += 'search_EQ_payType.id=' + ko.unwrap(data.searchModel.payType().id) + '&';
            param += 'search_EQ_payable=' + ko.unwrap(data.searchModel.payable()) + '&';
            param += 'search_EQ_payAmount=' + ko.unwrap(data.searchModel.payAmount()) + '&';
            param += 'search_EQ_purchaseDate=' + ko.unwrap(data.searchModel.purchaseDate()) + '&';
            param += 'search_EQ_expirationDay=' + ko.unwrap(data.searchModel.expirationDay()) + '&';
            param += 'search_EQ_expDate=' + ko.unwrap(data.searchModel.expDate()) + '&';
            param += 'search_EQ_purchaseTotal=' + ko.unwrap(data.searchModel.purchaseTotal()) + '&';
            param += 'search_EQ_restTotal=' + ko.unwrap(data.searchModel.restTotal()) + '&';
            param += 'search_EQ_medicine.id=' + ko.unwrap(data.searchModel.medicine().id);
            if(initParam) {
                param = initParam + '&' + param;
            }
            data.tableModel.param(param);
            model.tableModel.loadData(true);
        };

        /**
         * 详细
         */
        model.showData = function (data, event) {
            var viewModel = new Model().dialogModel;
            viewModel.stockModel = common.mappingData(common.extend(viewModel.stockModel, data));
            var dia = dialog({
                id: "showStockDialog",
                url: '/mdSheQuHospitalBase/stock/stock.show.tmpl.html',
                width: '600px',
                title: '库存表基本信息',
                okValue: '关闭',
                ok: function () {
 
                },
                onshow: function () {
 
                }
            });
            common.showModalDialog(dia, viewModel);
        };

        /**
         * 增加
         */
        model.addData = function(){
            var viewModel = new Model().dialogModel;
            modifyData(viewModel, '新建');
            model.tableModel.loadData(true);
        };

        /**
         * 修改
         */
        model.updateData = function (data, event) {
            var viewModel = new Model().dialogModel;
            viewModel.stockModel = common.mappingData(common.extend(viewModel.stockModel, data), ['payType', 'medicine']);
            modifyData(viewModel, '编辑');
        };

        /**
         * 增加或修改
         */
        function modifyData(viewModel, titlePrefix){
            viewModel.dictionaryArray(model.dialogModel.dictionaryArray());
            viewModel.medicineArray(model.dialogModel.medicineArray());
            var dia = dialog({
                id: 'saveStockDialog',
                url: '/mdSheQuHospitalBase/stock/stock.save.tmpl.html',
                width: '600px',
                title: titlePrefix + '库存表',
                okValue: '确定',
                cancelValue: '取消',
                ok: function () {
                    if(common.isErrors(viewModel.stockModel)){
                        var param = ko.mapping.toJSON(viewModel.stockModel);
                        service.saveData(param).success(function(data){
                            common.showAlertDialog('数据保存成功!', 'success');
                            model.tableModel.loadData(true);
                        });
                    }else{
                        return false;
                    }
                },
                cancel: function () {},
                onshow: function(){

                }
            });
            common.showModalDialog(dia, viewModel);
        }

        /**
         * 删除
         */
        model.deleteData = function (data, event) {
            common.showConfirmDialog('确定要删除吗?', function () {
                service.deleteDataById(data.id).success(function(data){
                    common.showAlertDialog('数据删除成功!', 'success');
                    model.tableModel.loadData(true);
                });
            });
        };

        return model;
    }
    return {
        initialize: initialize
    };
});